/* Accenture IDC,
   Bhumika Udani    v1.1              dt: 28/01/2011
   Trigger a task update Task Comments that got created as workflow action on meeting the Renewal Date
   */

trigger AASUpdateOrderInfoOnTask on Task (before insert) {
    list<id> OrderId = new list<id>();
    Map<id,Order__c> OrderMap = new Map<id,Order__c>();
    List<Task> TaskList =new List<Task>();
    List<Order__c> Orders = new List<Order__c>();
    //Map <String,Schema.RecordTypeInfo> recordTypes =  Task.sObjectType.getDescribe().getRecordTypeInfosByName();
    //Id AASRecordTypeID = recordTypes.get('Advanced Ad Solutions').getRecordTypeId();
    Id AASRecordTypeID = RecordTypeHelper.GetRecordTypeIDByDevName('Task',  'Advanced_Ad_Solutions');
    String Fulfillment_Order_Prefix = GeneralConstants.Fulfillment_Order_Prefix; 
    
    for(task t :trigger.new){
        if(t.recordtypeid == AASRecordTypeID && t.whatid!=null){
            String FOWhatId=t.Whatid;
            if(FOWhatId.startsWith(Fulfillment_Order_Prefix))
                OrderId.add(t.whatid);}
    }
    

    if(OrderId.size()>0)Orders  = [select opportunity__c,opportunity__r.Name,opportunity__r.accountid,opportunity__r.account.Name, product__c, Ordered_Product_Amount__c from order__c where id in : OrderId];
       for(Order__c O :Orders){
      OrderMap.put(O.id,O);
    }
    
    for(task tk :trigger.new){    
        if(tk.recordtypeid == AASRecordTypeID && tk.whatid!=null){
        string ordertask = tk.whatid;
        if(ordertask.startswith(Fulfillment_Order_Prefix)){
        Order__c O = OrderMap.get(tk.whatid);
        tk.OpportunityId__c = tk.Server_URL__c + O.opportunity__c;
        tk.Account_Link__c = tk.Server_URL__c + O.opportunity__r.accountid;
        //PageReference pageRef = system.currentPageReference();
        tk.Order_Details__c = 'Account Name: '+ O.opportunity__r.account.Name + '\n' + 'Opportunity: '+ O.opportunity__r.Name + '\n'+ 'Product: ' + O.product__c + '\n' + 'Amount: ' + O.Ordered_Product_Amount__c;         }}    
    }
}